package com.lyw.utils;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

// 技能
public class SkillUtil {

  // 技能框信息
  private static Rectangle skill_box;
  private static Rectangle character_box;
  private static Rectangle monster_box;
  private static final int SKILL_WIDTH = 216;
  private static final int SKILL_HEIGHT = 61;
  private static final int SKILL_X = 299;
  private static final int SKILL_Y = 533;
  // 技能坐标
  private static final int[][] SKILL_POSITION = {
          {10, 4}, {41, 4}, {72, 4}, {103, 4}, {124, 4}, {155, 4}, {186, 4},
          {10, 35}, {41, 35}, {72, 35}, {103, 35}, {124, 35}, {155, 35}, {186, 35}
  };
  // 可释放技能颜色
  private static final int[] SKILL_COLOR1 = {0, 255, 0};
  private static final int[] SKILL_COLOR2 = {102, 102, 102};
  // 所有技能能否释放的状态
  public static final ConcurrentHashMap<Integer, Boolean> SKILL_STATUS = new ConcurrentHashMap<>(20);
  // 技能优先级跟 -》 技能坐标 每个下标对应
  public static final List<Integer> SKILL_PRIORITY = Arrays.asList(
          KeyEvent.VK_Q, KeyEvent.VK_W, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_V, KeyEvent.VK_Y, KeyEvent.VK_T,
          KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H, KeyEvent.VK_7
  );

  static {
    // 技能初始化
    SKILL_STATUS.put(KeyEvent.VK_Q, true);
    SKILL_STATUS.put(KeyEvent.VK_W, true);
    SKILL_STATUS.put(KeyEvent.VK_E, true);
    SKILL_STATUS.put(KeyEvent.VK_R, true);
    SKILL_STATUS.put(KeyEvent.VK_V, true);
    SKILL_STATUS.put(KeyEvent.VK_Y, true);
    SKILL_STATUS.put(KeyEvent.VK_T, true);
    SKILL_STATUS.put(KeyEvent.VK_A, true);
    SKILL_STATUS.put(KeyEvent.VK_S, true);
    SKILL_STATUS.put(KeyEvent.VK_D, true);
    SKILL_STATUS.put(KeyEvent.VK_F, true);
    SKILL_STATUS.put(KeyEvent.VK_G, true);
    SKILL_STATUS.put(KeyEvent.VK_H, true);
    SKILL_STATUS.put(KeyEvent.VK_7, true);
  }

  private SkillUtil() {}

  /**
   * 准备开启技能扫描功能
   */
  public static void prepareEnableSkillScan(Rectangle rectangle) {
    skill_box = new Rectangle((int) rectangle.getX() + SKILL_X, (int) rectangle.getY() + SKILL_Y, SKILL_WIDTH, SKILL_HEIGHT);
  }

  /**
   * 更新角色技能
   */
  public static void getSkillPositionByColors(Robot robot) {

    BufferedImage screenCapture = robot.createScreenCapture(skill_box);
    for (int i = 0; i < SKILL_POSITION.length; i++) {
      int rgb = screenCapture.getRGB(SKILL_POSITION[i][0], SKILL_POSITION[i][1]);
      if (
              (((rgb >> 16) & 0xff) == SKILL_COLOR1[0]
                      && ((rgb >> 8) & 0xff) == SKILL_COLOR1[1]
                      && (rgb & 0xff) == SKILL_COLOR1[2])
                      ||
                      (((rgb >> 16) & 0xff) == SKILL_COLOR2[0]
                              && ((rgb >> 8) & 0xff) == SKILL_COLOR2[1]
                              && (rgb & 0xff) == SKILL_COLOR2[2])
      ) {
        SKILL_STATUS.put(SKILL_PRIORITY.get(i), true);
      }
    }
  }

  public static void releasedSkill(int keyboard, int[] skill_args) throws Exception {
    MouseAndKeyEventUtil.keyEnter(KeyEvent.VK_RIGHT);
    for (int i = 0; i < skill_args[1]; i++) {
      if (skill_args[2] == 1) {
        TimeUnit.MILLISECONDS.sleep(20);
        MouseAndKeyEventUtil.keyPress(KeyEvent.VK_RIGHT);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_RIGHT);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyPress(KeyEvent.VK_LEFT);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_LEFT);
        /*MouseAndKeyEventUtil.keyPress(KeyEvent.VK_RIGHT);
        MouseAndKeyEventUtil.keyPress(KeyEvent.VK_DOWN);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_RIGHT);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_DOWN);
        MouseAndKeyEventUtil.keyPress(KeyEvent.VK_UP);
        MouseAndKeyEventUtil.keyPress(KeyEvent.VK_LEFT);
        TimeUnit.MILLISECONDS.sleep(60);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        MouseAndKeyEventUtil.keyEnter(keyboard);
        TimeUnit.MILLISECONDS.sleep(80);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_LEFT);
        TimeUnit.MILLISECONDS.sleep(50);
        MouseAndKeyEventUtil.keyReleased(KeyEvent.VK_UP);*/
      } else if (skill_args[2] > 2) {
        for (int j = 0; j < skill_args[2]; j++) {
          TimeUnit.MILLISECONDS.sleep(40);
          MouseAndKeyEventUtil.keyEnter(keyboard);
        }
      } else {
        TimeUnit.MILLISECONDS.sleep(40);
        MouseAndKeyEventUtil.keyEnter(keyboard);
      }
    }
  }
}
